package com.wcy.controller;

import com.alibaba.nacos.api.exception.NacosException;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wcy.domain.AdminBank;
import com.wcy.model.R;
import com.wcy.service.AdminBankService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.constraints.NotBlank;

/**
 * <p>Title:AdminBankController.java</p >
 * <p>Description: </p >
 * <p>Copyright: 公共服务与应急管理战略业务本部 Copyright(c)2020</p >
 * <p>Date:2021/9/27 21:46</p >
 *
 * @author wcy (wangchaoyanga@mail.taiji.com.cn)
 * @version 1.0
 */
@RestController
@RequestMapping("/adminBanks")
@Api(tags = "公司银行卡的配置")
public class AdminBankController {

    @Autowired
    private AdminBankService adminBankService;

    @GetMapping
    @ApiOperation("分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current",value = "当前页"),
            @ApiImplicitParam(name = "size",value = "每页大小"),
            @ApiImplicitParam(name = "bankCard",value = "银行卡号")
    })
    @PreAuthorize("hasAuthority('admin_bank_query')")
    public R<Page<AdminBank>> findByPage(@ApiIgnore Page<AdminBank> page,String bankCard){
        Page<AdminBank> adminBankPage=adminBankService.findByPage(page,bankCard);
        return R.ok(adminBankPage);
    }

    @PostMapping
    @ApiOperation(value = "新增银行卡")
    @PreAuthorize("hasAuthority('admin_bank_create')")
    public R add(@RequestBody @Validated AdminBank adminBank){
        boolean save = adminBankService.save(adminBank);
        if(save){
            return R.ok();
        }
        return R.fail("新增失败");
    }

    @PatchMapping
    @ApiOperation(value = "更新银行卡")
    @PreAuthorize("hasAuthority('admin_bank_update')")
    public R update(@RequestBody @Validated AdminBank adminBank){
        boolean update = adminBankService.updateById(adminBank);
        if(update){
            return R.ok();
        }
        return R.fail("更新失败");
    }

    @PostMapping("/adminUpdateBankStatus")
    @ApiOperation("修改状态")
    @PreAuthorize("hasAuthority('work_issue_update')")
    @Validated
    public R updateStatus(@RequestParam @NotBlank Long bankId, @RequestParam @NotBlank String status){
        AdminBank adminBank=new AdminBank();
        adminBank.setId(bankId);
        adminBank.setStatus(Byte.valueOf(status));
        boolean update = adminBankService.updateById(adminBank);
        if(update){
            return R.ok();
        }
        return R.fail("修改失败");
    }

}
